﻿using CoreShop.Model.FromBody;
using CoreShop.Model.UI;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Policy;
using System.Text;
using System.Threading.Tasks;

namespace CoreShop.Services
{
    public class ApiRemoteServices
    {
        public string sUrl = "http://adminapi.pos08.com";
        //用户名：100120，密码：666666
        public string token = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJodHRwOi8vc2NoZW1hcy54bWxzb2FwLm9yZy93cy8yMDA1LzA1L2lkZW50aXR5L2NsYWltcy9naXZlbm5hbWUiOiIxMDAxMjAiLCJodHRwOi8vc2NoZW1hcy54bWxzb2FwLm9yZy93cy8yMDA1LzA1L2lkZW50aXR5L2NsYWltcy9uYW1lIjoiMTAwMTIwIiwianRpIjoiMTMyNiIsInN0b3JlSWQiOiIxMDU2IiwiY29ycElkIjoiMCIsInNwbGl0SWQiOiIyMDIzMDkiLCJzaWduIjoie1wic3RvcmVJZFwiOjEwNTYsXCJ1c2VySWRcIjoxMzI2LFwiY29ycElkXCI6MH0iLCJodHRwOi8vc2NoZW1hcy5taWNyb3NvZnQuY29tL3dzLzIwMDgvMDYvaWRlbnRpdHkvY2xhaW1zL2V4cGlyYXRpb24iOiIyMDI1LzcvNSAxNjozOTozNiIsImh0dHA6Ly9zY2hlbWFzLm1pY3Jvc29mdC5jb20vd3MvMjAwOC8wNi9pZGVudGl0eS9jbGFpbXMvcm9sZSI6IjIyIiwibmJmIjoxNzQ5MTEyNzc2LCJleHAiOjE3NTE3MDQ3NzYsImlzcyI6IkNvcmVTaG9wUHJvZmVzc2lvbmFsIiwiYXVkIjoiQ29yZUNtcyJ9.9Rc90uU3cjaYNoHMyT_AJ1qIa0igVh_WC73UDfDk6Y4";

        /// <summary>
        /// 获取打印模板库
        /// </summary>
        /// <param name="userGrade"></param>
        /// <param name="token"></param>
        /// <param name="msg"></param>
        /// <returns></returns>
        public List<corecmsprintbilltemplatetype> GetBillTemplateTypeList(ref string msg)
        {
            try
            {
                msg = "获取失败。";
                string sWebUrl = sUrl + "/Api/corecmsprintbilltemplatetype/GetPageList";
                string postStr = "pageNum={0}&pageSize={1}";
                postStr = string.Format(postStr, 1, 200);
                string retStr = Library.HttpHelper.httpGet(sWebUrl, postStr, token);
                AdminUiCallBack res = JsonConvert.DeserializeObject<AdminUiCallBack>(retStr);
                msg = res.msg;
                if (res.code == 0)
                {
                    return JsonConvert.DeserializeObject<List<corecmsprintbilltemplatetype>>(GetListFromCallBack(retStr));
                }
            }
            catch (Exception ex)
            {
                msg = "系统出错：" + ex.Message;
            }
            return null;
        }

        public string GetListFromCallBack(string retStr)
        {
            AdminUiCallBack adminUiCallBack = JsonConvert.DeserializeObject<AdminUiCallBack>(retStr);
            if ((adminUiCallBack == null) || (adminUiCallBack.code != 0)) return null;
            dynamic dynamicObj = JsonConvert.DeserializeObject<dynamic>(retStr);
            return Convert.ToString(dynamicObj.data.list);
        }

        /// <summary>
        /// 保存模板到服务器
        /// </summary>
        /// <param name="member"></param>
        /// <param name="token"></param>
        /// <param name="msg"></param>
        /// <returns></returns>
        public bool DoCreateTemplate(FMTemplate model, ref string msg)
        {
            try
            {

                string sWebUrl = sUrl + "/api/corecmsprintbilltemplate/DoCreateBatch";
                string postStr = JsonConvert.SerializeObject(model);
                string retStr = Library.HttpHelper.httpPost(sWebUrl, postStr, token);
                AdminUiCallBack res = JsonConvert.DeserializeObject<AdminUiCallBack>(retStr);
                if (res.code == 0) return true;
                return false;
            }
            catch (Exception)
            {

                throw;
            }
        }


    }
}
